#include "UIScrollContainer.h"
#include "comm.h"
#include "win_gdi.h"

UIScrollContainer::UIScrollContainer(UIContainer *parent) : UIContainer(parent)
{
    setName("UIScrollContainer");
    setPadding(DuiCorner{4, 4, 4, 4});
}

UIScrollContainer::~UIScrollContainer()
{
}

void UIScrollContainer::onDrawContent(DuiWinManager *mgr, DuiRect &rect_in, DuiRect &rect_out)
{
    // fix no setting viewport
    if (m_ScrollViewport.w == 0 || m_ScrollViewport.h == 0)
    {
        m_ScrollViewport = m_Rect;
    }
    // 绘制滚动条
    if (m_ScrollBarShowX)
    {
        auto rc = m_Rect;
        auto w1 = rc.w * rc.w / m_ScrollViewport.w; // 滚动条指示器长度
        auto h1 = m_ScrollBarWidth;                 // 滚动条高度
        if (rc.w >= m_ScrollViewport.w)
        {
            m_ScrollBarRadX = 1.0f;
        }
        else
        {
            m_ScrollBarRadX = (float)(rc.w - w1) / (m_ScrollViewport.w - rc.w); // 比例
        }
        // 背景
        auto rc2 = DuiRect{rc.x, rc.y + rc.h - h1, rc.w - m_ScrollBarWidth, h1};
        render_draw_fill(mgr, rc2, DuiColor{0xDD, 0xDD, 0xDD, 200}, 0);
        // 滚动条指示器
        auto x1 = rc.x + m_ScrollBarPosX * m_ScrollBarRadX; // 滚动条位置
        if (x1 > (rc.x + rc.w - w1 - m_ScrollBarWidth))
        {
            x1 = rc.x + rc.w - w1 - m_ScrollBarWidth;
        }
        auto y1 = rc.y + rc.h - h1 + 2; // 滚动条位置
        m_ScrollBarRectX = DuiRect{(int)x1, y1, w1, h1 - 3};
        render_draw_fill(mgr, m_ScrollBarRectX, m_ScrollBarColor, 6);
    }
    if (m_ScrollBarShowY)
    {
        auto rc = m_Rect;
        auto w1 = m_ScrollBarWidth;                 // 滚动条宽度
        auto h1 = rc.h * rc.h / m_ScrollViewport.h; // 滚动条指示器长度
        if (rc.h >= m_ScrollViewport.h)
        {
            m_ScrollBarRadY = 1.0f;
        }
        else
        {
            m_ScrollBarRadY = (float)(rc.h - h1) / (m_ScrollViewport.h - rc.h); // 比例
        }
        // 背景
        auto rc2 = DuiRect{rc.x + rc.w - w1, rc.y, w1, rc.h - m_ScrollBarWidth};
        render_draw_fill(mgr, rc2, DuiColor{0xDD, 0xDD, 0xDD, 200}, 0);
        // 滚动条指示器
        auto x1 = rc.x + rc.w - w1 + 2;                     // 滚动条位置
        auto y1 = rc.y + m_ScrollBarPosY * m_ScrollBarRadY; // 滚动条位置
        if (y1 > (rc.y + rc.h - h1 - m_ScrollBarWidth))
        {
            y1 = rc.y + rc.h - h1 - m_ScrollBarWidth;
        }
        m_ScrollBarRectY = DuiRect{x1, (int)y1, w1 - 3, h1};
        render_draw_fill(mgr, m_ScrollBarRectY, m_ScrollBarColor, 6);
    }
}

std::string UIScrollContainer::getUIType()
{
    return "UIScrollContainer";
}

void UIScrollContainer::onMouseDown(int x, int y)
{
    if (m_ScrollBarShowX)
    {
        if (y > m_ScrollBarRectX.y)
        {
            if (dui_comm_pt_in_rect(x, y, m_ScrollBarRectX))
            {
                m_bScrollBarPressedX = true;
                m_ScrollBarPressedXPos = x;
                m_ScrollBarViewXPos = m_ScrollBarPosX;
            }
            else
            {
                // click nav
                auto navX = x - m_Rect.x - m_ScrollBarRectX.w / 2;
                m_ScrollBarPosX = (float)(navX) / m_ScrollBarRadX;
                if (m_ScrollBarPosX < 0)
                {
                    m_ScrollBarPosX = 0;
                }
                if (m_ScrollBarPosX > m_ScrollViewport.w)
                {
                    m_ScrollBarPosX = m_ScrollViewport.w;
                }
            }
        }
        printf("X pressed, x=%d\n", x);
    }
    if (m_ScrollBarShowY)
    {
        if (x > m_ScrollBarRectY.x)
        {
            if (dui_comm_pt_in_rect(x, y, m_ScrollBarRectY))
            {
                m_bScrollBarPressedY = true;
                m_ScrollBarPressedYPos = y;
                m_ScrollBarViewYPos = m_ScrollBarPosY;
            }
            else
            {
                // click nav
                auto navY = y - m_Rect.y - m_ScrollBarRectY.h / 2;
                m_ScrollBarPosY = (float)(navY) / m_ScrollBarRadY;
                if (m_ScrollBarPosY < 0)
                {
                    m_ScrollBarPosY = 0;
                }
                if (m_ScrollBarPosY > m_ScrollViewport.h)
                {
                    m_ScrollBarPosY = m_ScrollViewport.h;
                }
            }
        }
        printf("Y pressed, y=%d\n", y);
    }
}

void UIScrollContainer::onMouseUp(int x, int y)
{
    m_bScrollBarPressedX = false;
    m_bScrollBarPressedY = false;
    printf("released, x=%d, y=%d\n", x, y);
}

void UIScrollContainer::onMouseWheel(int delta, int x, int y)
{
    if (m_ScrollBarShowX)
    {
        if (y > (m_Rect.y + m_Rect.h - m_ScrollBarWidth))
        {
            // wheel nav
            m_ScrollBarPosX += (delta > 0 ? -1 : 1) * m_Rect.w / 2;
            if (m_ScrollBarPosX < 0)
            {
                m_ScrollBarPosX = 0;
            }
            if (m_ScrollBarPosX > m_ScrollViewport.w)
            {
                m_ScrollBarPosX = m_ScrollViewport.w;
            }
        }
        // printf("X onMouseWheel, x=%d\n", x);
    }
    if (m_ScrollBarShowY)
    {
        if (x > (m_Rect.x + m_Rect.w - m_ScrollBarWidth))
        {
            // wheel nav
            m_ScrollBarPosY += (delta > 0 ? -1 : 1) * m_Rect.h / 2;
            if (m_ScrollBarPosY < 0)
            {
                m_ScrollBarPosY = 0;
            }
            if (m_ScrollBarPosY > m_ScrollViewport.h)
            {
                m_ScrollBarPosY = m_ScrollViewport.h;
            }
        }
        // printf("Y onMouseWheel, y=%d\n", y);
    }
}

void UIScrollContainer::onMouseMove(int x, int y)
{
    if (m_bScrollBarPressedX)
    {
        m_ScrollBarPosX = m_ScrollBarViewXPos + (float)(x - m_ScrollBarPressedXPos) / m_ScrollBarRadX;

        printf("move x=%d, x2: %d\n", x - m_ScrollBarPressedXPos, m_ScrollBarPosX);
        if (m_ScrollBarPosX < 0)
        {
            m_ScrollBarPosX = 0;
        }
        if (m_ScrollBarPosX > m_ScrollViewport.w)
        {
            m_ScrollBarPosX = m_ScrollViewport.w;
        }
    }
    if (m_bScrollBarPressedY)
    {
        m_ScrollBarPosY = m_ScrollBarViewYPos + (float)(y - m_ScrollBarPressedYPos) / m_ScrollBarRadY;
        printf("move y=%d, y2: %d\n", y - m_ScrollBarPressedYPos, m_ScrollBarPosY);
        if (m_ScrollBarPosY < 0)
        {
            m_ScrollBarPosY = 0;
        }
        if (m_ScrollBarPosY > m_ScrollViewport.h)
        {
            m_ScrollBarPosY = m_ScrollViewport.h;
        }
    }
}

void UIScrollContainer::onMouseEnter()
{
}

void UIScrollContainer::onMouseLeave()
{
    m_bScrollBarPressedX = false;
    m_bScrollBarPressedY = false;
}
